package com.tim.servlet;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.tim.pojo.DataResult;
import com.tim.pojo.Pet;
import com.tim.service.Impl.PetServiceImpl;
import com.tim.service.PetService;
import com.tim.util.FileUploadUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 作者：Tim
 * 时间：2021/4/29 11:10
 */
@WebServlet("/pet/*")
@MultipartConfig
public class PetServlet extends BaseServlet{
    PetService petService = new PetServiceImpl();
    /**
     * 分页查询所有
     */
    public DataResult all(HttpServletRequest request, HttpServletResponse response) {
        //1、获取请求参数
        String page = request.getParameter("page");
        String limit=request.getParameter("limit");
        //获取模糊查询的数据
        String owner = request.getParameter("owner");
        //2、调用service业务方法
        DataResult dataResult = petService.limit(page, limit,owner);
        //3、做出响应
        return dataResult;
    }

    /**
     * 删除
     */
    public DataResult del(HttpServletRequest request,HttpServletResponse response){
        String id = request.getParameter("id");
        int count = petService.deletePet(id);
        DataResult dataResult;
        if(count>0){
            dataResult= new DataResult(0,"删除成功");
        }else{
            dataResult= new DataResult(40000,"删除成功");
        }
        return dataResult;
    }

    /**
     * 添加
     */
    public DataResult add(HttpServletRequest request,HttpServletResponse response) throws IOException {
        String info = request.getParameter("info");
        //将前端传递的json字符串转换成java对象
        ObjectMapper mapper = new ObjectMapper();
        Pet pet = mapper.readValue(info, Pet.class);
        int count = petService.addPet(pet);
        DataResult dataResult;
        if(count>0){
            dataResult = new DataResult(0,"添加成功");
        }else{
            dataResult= new DataResult(40000,"添加失败");
        }
        return dataResult;
    }

    /**
     * 根据ID进行查询
     */
    public DataResult one(HttpServletRequest request,HttpServletResponse response){
        String id = request.getParameter("id");
        Pet pet= petService.selectOne(id);
        DataResult dataResult;
        if (pet==null){
            dataResult = new DataResult(40000,"查无结果");
        }
        dataResult = new DataResult(0,"查询成功",pet);
        return dataResult;
    }

    /**
     * 修改编辑
     */
    public DataResult update(HttpServletRequest request,HttpServletResponse response) throws IOException {
        String info = request.getParameter("info");
        ObjectMapper mapper = new ObjectMapper();
        Pet pet = mapper.readValue(info, Pet.class);
        int count  = petService.updatePet(pet);
        DataResult dataResult;
        if(count>0){
            dataResult = new DataResult(0,"修改成功");
        }else{
            dataResult=new DataResult(40000,"修改失败");
        }
        return dataResult;
    }
    /**
     * 批量删除
     */
    public DataResult delAll(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //1、获取请求参数（ids） JSON字符串（数组）
        String ids = request.getParameter("ids");
        ObjectMapper mapper = new ObjectMapper();
        String[] idArr = mapper.readValue(ids, String[].class);

        int count = petService.deleteAll(idArr);
        DataResult dataResult;
        if(count>0){
            dataResult = new DataResult(0,"删除成功");
        }else{
            dataResult = new DataResult(40000,"删除失败");
        }
        return dataResult;
    }

    /**
     * 上传图片
     */
    public DataResult upload(HttpServletRequest request, HttpServletResponse response)  throws ServletException, IOException {
        DataResult dataResult;
        try {
            //调用工具类上传文件
            String sqlPath = FileUploadUtil.upload(request);
            //将地址返回到前端
            dataResult = new DataResult(0,"上传成功",sqlPath);
        } catch (Exception e) {
            e.printStackTrace();
            dataResult = new DataResult(40000,"上传失败");
        }
        return dataResult;
    }
}
